open Fake.IO
open Fake.Core
open Fake.DotNet

#r "paket:
nuget Fake.IO.FileSystem
nuget Fake.DotNet.MSBuild
nuget Fake.Core.Target //"
#load "./.fake/build.fsx/intellisense.fsx"

//Properties
let buildDir = "./build/"
let version = "1.3.9"
let appDir = "./../unpackage/dist/build/h5/"

//模块包仓储信息
let apiKey = "wokanbit_nuget"

//Targets
Target.create "Clean" (fun _ ->
    Shell.cleanDir buildDir
)

//将jinlin.mp项目打包成模块包并上传到模块仓储中
Target.create "jinlin.mp" (fun _ ->

    Trace.trace("准备打包jinlin.mp模块包...");
    let packResult =
        CreateProcess.fromRawCommand "plugininstaller" ["pack";appDir; "./metadata/jinlin.mp.json"; "--targetDirectory=" + buildDir]
        |> CreateProcess.redirectOutput
        |> Proc.run

    if packResult.ExitCode <> 0 then
        printfn "%s" packResult.Result.Output
        failwithf "打包jinlin.mp失败，返回值:%d,错误信息:%s" packResult.ExitCode packResult.Result.Error
    else 
        let output = packResult.Result.Output
        Trace.trace("打包成功," + output)

    Trace.trace("准备将打包好的jinlin.mp模块包发布到模块仓储中...")
    let publishResult =
        CreateProcess.fromRawCommand "plugininstaller" ["push"; "./build/jinlin.mp-" + version + ".upack"; "--apiKey=" + apiKey;"--sourceFeedName=app"]
        |> CreateProcess.redirectOutput
        |> Proc.run
    if publishResult.ExitCode <> 0 then
        printfn "%s" publishResult.Result.Output
        failwithf "发布jinlin.mp模块包到模块仓储失败，返回值:%d,错误信息:%s" publishResult.ExitCode publishResult.Result.Error
    else 
        let output = publishResult.Result.Output
        Trace.trace("发布jinlin.mp模块包到模块仓储成功," + output)
)

//Default target
Target.create "End" (fun _ ->
    Trace.trace "构建完成，byebye"
)

//Dependencies
open Fake.Core.TargetOperators

"Clean" 
    ==> "jinlin.mp"
    ==> "End"

//sart build
Target.runOrDefault "End"